﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using System.Collections;
using System.Data.Objects;
using System.Data.Objects.SqlClient;
using XBoshiGFS.forms;
using CrystalDecisions.Shared;
using Microsoft.Win32;


namespace XBoshiGFS.reports
{
    public partial class WaterUsageBillsTestForm : Form
    {
        XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();
        IQueryable  orgSql;
        String strGroupID;
        IList<WaterUsageBillsReportData> printDataList = new List<WaterUsageBillsReportData>();
        String strUserName = "";



        public WaterUsageBillsTestForm()
        {
            InitializeComponent();

            cmbStation.DataSource = (from p in context.StationSet select p);

            //灌季
            cmbWaterSeason.DataSource = (from p in context.WaterSeasonSet orderby p.strYear descending ,p.strSeason descending  select p);

            var strUserName = (from p in context.UsersSet where p.strCode == MainMDIParent.currentUserCode select p.strName).FirstOrDefault();



        }
 
        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
                strGroupID = "-1";
                 try
                {
                 //orgSql = 
                 //            from  t in context.PeopleSet
                 //            join o in context.GroupSet on t.GroupId equals o.Id  
                 //            join p in context.VillageSet on o.VillageId equals p.Id
                 //            join q in context.WaterCourseSet on p.WaterCourseId equals q.Id
                 //            join r in context.SegmentCourseSet on q.SegmentCourseId equals r.Id
                 //            join s in context.StationSet on r.StationId equals s.Id
                 //            where p.Id == this.strGroupID
                 //            select new  {
                 //                t.strCode, t.strName ,
                 //                strGropId =p.Id,strGroupName =p.strName ,strGroupCode =p.strCode ,
                 //                strVillageCode =o.strCode ,strVillageName =o.strName ,
                 //                strWaterCourseCode =q.strCode ,strWaterCourseName  =q.strName ,
                 //                strSegmentCode =r.strCode ,strSegmentName =r.strName ,
                 //                strStationCode =s.strCode ,strStationName=s.strName 
                 //             };

                 //var orgSql = GetQuerySql("-1");



                   for (var i = 0; i < 200; i++)
                   {
                       WaterUsageBillsReportData wprd = new WaterUsageBillsReportData();

                       wprd.strStationName = "st_" + i;
                           printDataList.Add(wprd);
                    }
                   WaterUsageBillsTest1.SetDataSource(printDataList);
                   WaterUsageBillsTest1.SetParameterValue("IsPrint", false);
                }
                catch (Exception ex) {

                    var a = ex.InnerException;
                } 
        }


        private void cmbStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbSegment.DataSource = (from p in context.SegmentCourseSet where p.StationId == strID orderby p.strName select p);
        }

        private void cmbSegment_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbWaterCourse.DataSource = (from p in context.WaterCourseSet where p.SegmentCourseId == strID orderby p.strName select p);

        }

        private void cmbWaterCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbVillage.DataSource = (from p in context.VillageSet where p.WaterCourseId == strID orderby p.strName select p);

        }

        private void cmbVillage_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbGroup.DataSource = (from p in context.GroupSet where p.VillageId == strID orderby p.strName select p);

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
          //  printDataList.Clear();

            try
            {


                this.Cursor = Cursors.WaitCursor;   
                strGroupID = (String)cmbGroup.SelectedValue;
              //形成打印数据
            //    var sql = GetQuerySql(strGroupID);
                crystalReportViewer1.ReportSource = WaterUsageBillsTest1;
                WaterUsageBillsTest1.SetDataSource(printDataList);
                WaterUsageBillsTest1.SetParameterValue("IsPrint", false);
                WaterUsageBillsTest1.Refresh();
                //WaterUsageBills1.SetParameterValue("IsPrint", false);
                //crystalReportViewer1.RefreshReport();     


            }catch(Exception ex){
            
            }
            this.Cursor = Cursors.Default ;   
        }

        private IQueryable GetQuerySql(String strGroupID)
        {
            String strSeasonID="";
            String feeID = "";
            try
            {
                  WaterSeasonSet season = ((WaterSeasonSet)(cmbWaterSeason.SelectedItem));
                  //SeasonName = season.strName;
                  strSeasonID = season.Id;
                  feeID = season.FeeStandardSetId;
                 // PriceAll = String.Format("{0}", (from p in context.FeeStandardSet where p.Id == season.FeeStandardSetId select p.PriceAll).FirstOrDefault());
            }
            catch { 
            
            
            }
           var orgSql =
            from u in context.WaterUsageSet 
            join t in context.PeopleSet on  u.PeopleId  equals t.Id 
            join o in context.GroupSet on t.GroupId equals o.Id
            join p in context.VillageSet on o.VillageId equals p.Id
            join q in context.WaterCourseSet on p.WaterCourseId equals q.Id
            join r in context.SegmentCourseSet on q.SegmentCourseId equals r.Id
            join s in context.StationSet on r.StationId equals s.Id
            join fee in context.FeeStandardSet on u.FeeStandardId  equals fee.Id 
            join season in context.WaterSeasonSet on u.WaterSeasonId  equals season.Id 
            where o.Id == this.strGroupID  && season.Id == strSeasonID && u.FeePrintLogSet.Count <=0  && u.WaterCap >0
            orderby t.strCode 
            select new // WaterUsageBillsReportData 
            {
                strPeopleID = t.Id,
                strPeopleCode= t.strCode,
                strPeopleName=t.strName,
                strGropId = p.Id,
                strGroupName = o.strName,
                strGroupCode = o.strCode,
                strVillageCode = p.strCode,
                strVillageName = p.strName,
                strWaterCourseCode = q.strCode,
                strWaterCourseName = q.strName,
                strSegmentCode = r.strCode,
                strSegmentName = r.strName,
                strStationCode = s.strCode,
                strStationName = s.strName,
                strGroupManager = o.strManager,
                strWaterSeasonName =season.strName,
                strFeeStardName =fee.strName ,
                decUseArea =u.UseArea.Value  ,
                decWaterBill =u.WaterBill.Value ,
                decWaterCap =u.WaterCap.Value  ,
                decReceivedBill =u.WaterBill.Value  ,
                decFlowRate =u.FlowRate.Value ,
                decPriceAll =fee.PriceAll.Value ,
                decRemain =0.0M,
                intUseLong =u.UseLong.Value ,
                datBeginTime = u.BeginTime==null?DateTime.Now:u.BeginTime.Value,
                strWaterUsageID=u.Id 
            };
           printDataList.Clear(); 
           foreach (var wub in orgSql) {
               WaterUsageBillsReportData tmp = new WaterUsageBillsReportData
               {
                   strPeopleID = wub.strPeopleID,
                   strPeopleCode = wub.strPeopleCode,
                   strPeopleName = wub.strPeopleName,
                   strGropId = wub.strGropId,
                   strGroupName = wub.strGroupName,
                   strGroupCode = wub.strGroupCode,
                   strVillageCode = wub.strVillageCode,
                   strVillageName = wub.strVillageName,
                   strWaterCourseCode = wub.strWaterCourseCode,
                   strWaterCourseName = wub.strWaterCourseName,
                   strSegmentCode = wub.strSegmentCode,
                   strSegmentName = wub.strSegmentName,
                   strStationCode = wub.strStationCode,
                   strStationName = wub.strStationName,
                   strGroupManager = wub.strGroupManager,
                   strWaterSeasonName = wub.strWaterSeasonName,
                   strFeeStardName = wub.strFeeStardName,
                   decUseArea = wub.decUseArea,
                   decWaterBill = wub.decWaterBill,
                   decWaterCap = wub.decWaterCap,
                   decReceivedBill = wub.decReceivedBill,
                   decFlowRate = wub.decFlowRate,
                   decPriceAll = wub.decPriceAll,
                   decRemain = wub.decRemain,
                   intUseLong = wub.intUseLong,
                   datBeginTime = wub.datBeginTime,
                   strWaterUsageID = wub.strWaterUsageID
 
               };

               printDataList.Add(tmp); 
           
           }
           return orgSql ;
        } 


        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }

        private void cmbWaterSeason_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnQuery.Enabled = true;
        }

        private void WaterBillReportForm_Load(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }



        private void setMargin(int i) {


                String printerName = "XPS";
                //设置参数，表示要正是打印
                WaterUsageBillsTest1.SetParameterValue("IsPrint", true);
                // WaterUsageBills1.Refresh();

                PageMargins margins;

                //   获取   PageMargins   结构并设置 
                //报表的边距。 
                margins = WaterUsageBillsTest1.PrintOptions.PageMargins;
                margins.bottomMargin = i*100;
                margins.leftMargin = 0;
                margins.rightMargin = 0;
                margins.topMargin = 0;
                //   应用页边距。 
                WaterUsageBillsTest1.PrintOptions.ApplyPageMargins(margins);

                //   选择打印机。 
                WaterUsageBillsTest1.PrintOptions.PrinterName = printerName;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

                 int firstCorrectPages=20;
                 int secondCorrectPages=200;

                 String strfcp = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "firstCorrectPages", "20");
                 String strscp = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "secondCorrectPages", "200");

                 firstCorrectPages = Int32.Parse(strfcp);
                 secondCorrectPages = Int32.Parse(strscp);


                 //firstCorrectPages = 1;
                 //secondCorrectPages = 1000;

                System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();

                int rawKind = 1; //记录标识自定义纸张的ID
                int paper1=0, paper2=0;
                //枚举出所有的打印纸尺寸 
                foreach(  System.Drawing.Printing.PaperSize paper in doc.PrinterSettings.PaperSizes){
                        
                    if (paper.PaperName.Equals("billReport")) {
                        paper1 = paper.RawKind;
                    }
                    if (paper.PaperName.Equals("billReport1"))
                    {
                        paper2 = paper.RawKind;
                    }
                }

                System.Drawing.Printing.PaperSize p = new System.Drawing.Printing.PaperSize("testPaper", 10, 10);
              




                ////修正纸张
                try
                {
                    WaterUsageBillsTest1.PrintOptions.PaperSize = (PaperSize)paper1;
                    WaterUsageBillsTest1.PrintToPrinter(1, true, 1, firstCorrectPages);
                }
                catch { }
                try
                {
                    WaterUsageBillsTest1.PrintOptions.PaperSize = (PaperSize)paper2;
                    WaterUsageBillsTest1.PrintToPrinter(1, true, firstCorrectPages + 1, firstCorrectPages + 1);
                }
                catch { }

                for (var i = firstCorrectPages + 2; i <= 200; i = i + secondCorrectPages)
                {
                    try
                    {
                        //正常纸张

                       // WaterUsageBillsTest1.PrintOptions.PaperSize = (PaperSize)p.RawKind;  

                        //setMargin(i);
                        WaterUsageBillsTest1.PrintOptions.PaperSize = (PaperSize)paper1;
                        WaterUsageBillsTest1.PrintToPrinter(1, true, i, i + secondCorrectPages - 2);
                        //修正纸张
                        //setMargin(i);
                        WaterUsageBillsTest1.PrintOptions.PaperSize = (PaperSize)paper2;
                        WaterUsageBillsTest1.PrintToPrinter(1, true, i + secondCorrectPages - 1, i + secondCorrectPages - 1);

                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Print(ex.Message);
                    }
                }
       
        }


    }
}
